﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Media.Animation;
using System.Windows.Data;
using System.Windows.Media;

namespace USpell.Controls
{
    public partial class GuessItem : UserControl
    {
        //滚动字幕动画
        Storyboard sb;
        //滚动字幕效果有快到慢
        DoubleAnimationUsingKeyFrames dani;
        SplineDoubleKeyFrame spf;

        /// <summary>
        /// 滚动字幕
        /// </summary>
        public string InfoText
        {
            get { return (string)GetValue(InfoTextProperty); }
            set
            {
                SetValue(InfoTextProperty, value);
            }
        }

        // Using a DependencyProperty as the backing store for InfoText.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty InfoTextProperty =
            DependencyProperty.Register("InfoText", typeof(string), typeof(GuessItem),null);





        public string CreateTime
        {
            get { return (string)GetValue(CreateTimeProperty); }
            set
            {
                SetValue(CreateTimeProperty, value);
               
            }
        }

        // Using a DependencyProperty as the backing store for CreateTime.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CreateTimeProperty =
            DependencyProperty.RegisterAttached("CreateTime", typeof(string), typeof(GuessItem), null);

        

        public GuessItem()
        {
            InitializeComponent();
            this.Loaded += GuessItem_Loaded;

            sb = new Storyboard();
            dani = new DoubleAnimationUsingKeyFrames();
            spf = new SplineDoubleKeyFrame();
            spf.KeySpline = new KeySpline() { ControlPoint1 = new Point(0, 0.5), ControlPoint2 = new Point(1, 0.4) };
            dani.KeyFrames.Add(spf);
            dani.RepeatBehavior = RepeatBehavior.Forever;
            Storyboard.SetTarget(dani, tb_GuessText);
            Storyboard.SetTargetProperty(dani, new PropertyPath(Canvas.LeftProperty));
            sb.Children.Add(dani);

        }

        void GuessItem_Loaded(object sender, RoutedEventArgs e)
        {
            string canStr = InfoText.Length < 80 ? InfoText : (InfoText.Substring(0, 80) + "...");   //textbook有长度限制
            tb_GuessText.Text = canStr + "   <<--我拼你猜-->>";
            spf.Value = -tb_GuessText.ActualWidth;
            var su = tb_GuessText.ActualWidth / 160;
            spf.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(su));
            if (tb_GuessText.ActualWidth < can_GuessItem.ActualWidth)//如果textblock可现实，就不动画
            {
                tb_GuessText.SetValue(Canvas.LeftProperty, 0d);
                return;
            }
            sb.Begin();
        }

        bool isxiala = true;
        private void Ellipse_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Storyboard xiala = new Storyboard();
            DoubleAnimation ani = new DoubleAnimation();
            DoubleAnimation ani2 = new DoubleAnimation();
            ani.Duration = new Duration(TimeSpan.FromSeconds(1.4));
            ani.To = isxiala ? 108 : 38;
            ani2.Duration = new Duration(TimeSpan.FromSeconds(0.4));
            ani.EasingFunction = new ElasticEase();
            ani2.To = isxiala ? 140 : 70;
            Storyboard.SetTarget(ani, canvas);
            Storyboard.SetTarget(ani2, can_GuessItem);
            Storyboard.SetTargetProperty(ani, new PropertyPath(FrameworkElement.HeightProperty));
            Storyboard.SetTargetProperty(ani2, new PropertyPath(FrameworkElement.HeightProperty));
            xiala.Children.Add(ani);
            xiala.Children.Add(ani2);
   
            if (isxiala)
                sb.Pause();
            else
                sb.Resume();

            isxiala = !isxiala;

            xiala.Begin();

        }

    }
}
